package com.nikkerball.web.base;

import com.nikkerball.web.converter.ComputationPage;
import com.nikkerball.web.page.HomePage;
import com.nikkerball.web.page.security.DetailsPage;
import com.nikkerball.web.page.security.SignInPage;
import org.apache.wicket.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.WebPage;

/**
 * Created by IntelliJ IDEA.
 * User: Marius
 * Date: 16-Jan-2010
 * Time: 11:08:29
 */
@SuppressWarnings({"ClassReferencesSubclass"})
public abstract class BasePage extends WebPage {

    protected BasePage() {

        StumpsLink<HomePage> homeLink = new StumpsLink<HomePage>("homeLink", HomePage.class, getString("link.home"));
        StumpsLink<ComputationPage> converterLink = new StumpsLink<ComputationPage>("converterLink",
                ComputationPage.class,
                getString("link.converter"));

        StumpsLink<SignInPage> signInLink = new StumpsLink<SignInPage>("signInLink", SignInPage.class,
                getString("link.signIn"));
        signInLink.setVisible(!this.isSignedIn());// Only visible when not signed in

        StumpsLink<HomePage> signOutLink = new StumpsLink<HomePage>("signOutLink", HomePage.class,
                getString("link.signOut"));
        // When clicking the sign out link we want to invalidate the session
        signOutLink.addClickListener(new StumpsLinkClickListener() {
            public void linkClicked() {
                getSession().invalidate();
            }
        });
        signOutLink.setVisible(this.isSignedIn());// Only visible when signed in

        StumpsLink<DetailsPage> detailsLink = new StumpsLink<DetailsPage>("detailsLink", DetailsPage.class,
                getString("link.details"));
        detailsLink.setVisible(this.isSignedIn());// Only visible when signed in

        add(homeLink);
        add(converterLink);
        add(signInLink);
        add(signOutLink);
        add(detailsLink);
    }

    protected boolean isSignedIn() {
        return ((AuthenticatedWebSession) getSession()).isSignedIn();
    }

}
